package org.bookie.model;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

@Entity
@Table(name="Outcomes")
public class Outcome extends LabeledEntity {
	private static final long serialVersionUID = 4655002084502665671L;
	
	/** Bet this Outcome belongs to */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="BetID")
    @NotNull
	private Bet bet;

	/** Odd of the Outcome to become successful */
    @Basic @NotNull private Double odds;
    
    protected Outcome(){super();}
    
	/**
	 * Constructs a new instance of Outcome
	 * 
	 * @param label label to set
	 * @param odds odds to set
	 */
	public Outcome(String label, Double odds) {
		setLabel(label);
		setOdds(odds);
	}
	
	/**
	 * Returns whether this Outcome is the result of its {@link Bet}.
	 * @return <code>true</code> if the outcome is the result of its event.
	 * <code>false</code> if the outcome is not the result of its event or if the result is <code>null</code>.
	 */
	public boolean isSuccessful(){
		Outcome success = getBet().getSuccessfulOutcome();
		return success!=null && getId().equals(success.getId());
	}
	
	/**
	 * Returns the odd of the Outcome
	 */
	public Double getOdds() {
		return odds;
	}

	/**
	 * Set the odd of the Outcome 
	 * @param odd odd to set
	 */
	public void setOdds(Double odd) {
		this.odds = odd;
	}
	
	/**
	 * Get the {@link Bet} this Outcome belongs to
	 * 
	 * @return instance of Bet
	 */
	public Bet getBet() {
		return bet;
	}

	/**
	 * Set the {@link Bet} this Outcome belongs to
	 * 
	 * @param bet bet to set
	 */
	public void setBet(Bet bet) {
		this.bet = bet;
	}

}
